package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishesMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @ClassName: ShoppingCartServiceImpl
 * @Author: YuanDing
 * @Date: 2024/2/6 17:38
 * @Description:
 */

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishesMapper dishMapper;

    @Override
    public void addOrUpdateShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCart(shoppingCartDTO);
        // 如果数据已存在则更新数量
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            shoppingCartList.forEach( item -> {
                shoppingCart.setNumber(item.getNumber());
                shoppingCart.setId(item.getId());
                shoppingCart.setNumber(item.getNumber() + 1);
                shoppingCartMapper.updateShoppingCart(shoppingCart);
            });
        } else {
            shoppingCart.setUserId(currentId);
            BeanUtils.copyProperties(shoppingCartDTO , shoppingCart);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            // 不存在则插入
            if (shoppingCartDTO.getSetmealId() != null) {
                // 如果是套餐
                Setmeal setmeal = setmealMapper.selectSetmealById(shoppingCartDTO.getSetmealId());
                BeanUtils.copyProperties(setmeal , shoppingCart);
                shoppingCart.setAmount(setmeal.getPrice());
            } else {
                // 如果是菜品
                Dish dish = dishMapper.selectDishById(shoppingCartDTO.getDishId());
                BeanUtils.copyProperties(dish , shoppingCart);
                shoppingCart.setAmount(dish.getPrice());
            }
            shoppingCartMapper.insertShoppingCart(shoppingCart);
        }


    }

    @Override
    public Result list() {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCartListByUserId(BaseContext.getCurrentId());
        return Result.success(shoppingCartList);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        if (shoppingCartDTO.getDishId() != null) {
            ShoppingCart shoppingCart = shoppingCartMapper
                    .selectShoppingCartByUserIdAndDishId(BaseContext.getCurrentId() , shoppingCartDTO.getDishId());
            if (shoppingCart.getNumber() == 1) {
                shoppingCartMapper.deleteShoppingCartById(shoppingCart.getId());
            } else {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateShoppingCart(shoppingCart);
            }

        } else {
            ShoppingCart shoppingCart = shoppingCartMapper
                    .selectShoppingCartBysetmealIdAndDishId(BaseContext.getCurrentId() , shoppingCartDTO.getSetmealId());
            if (shoppingCart.getNumber() == 1) {
                shoppingCartMapper.deleteShoppingCartById(shoppingCart.getId());
            } else {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateShoppingCart(shoppingCart);
            }
        }
    }

    @Override
    public void clean() {
        shoppingCartMapper.deleteAllByUserId(BaseContext.getCurrentId());
    }
}
